package com.maxd.service.helpTask;

import com.maxd.model.HelpClassify;
import com.maxd.model.HelpClassifyDetails;
import com.maxd.model.HelpClassifyModel;
import com.maxd.model.HelpTask;
import com.maxd.respository.helpTask.HelpClassifyJpaRepository;
import com.maxd.respository.helpTask.HelpClassifyRepository;
import com.maxd.respository.helpTask.HelpTaskRepository;
import com.maxd.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 一级分类
 */
@Service
public class HelpClassifyServiceImpl implements IHelpClassifyService {

    @Autowired
    private HelpClassifyJpaRepository helpClassifyJpaRepository;
    @Autowired
    private HelpClassifyRepository helpClassifyRepository;
    @Autowired
    private HelpTaskRepository helpTaskRepository;
    @Autowired
    private HelpClassifyDetailsService helpClassifyDetailsService;

    @Override
    public Page<HelpTask> selectHelpTaskByClassify(Long classifyDetailsId, Pageable pageable){
        return helpTaskRepository.selectByClassifyDetailsId(classifyDetailsId,pageable);
    }

    @Override
    public Page<HelpTask> selectByTitle(String title, Pageable pageable){
        return helpTaskRepository.selectByTitle(title,pageable);
    }


    @Override
    public List<HelpClassifyModel> getHelpClassifyModel(){
        List<HelpClassify> list1=helpClassifyRepository.selectList(2);
        List<HelpClassifyModel> list3= BeanHelper.copyWithCollection(list1,HelpClassifyModel.class);
        List<HelpClassifyDetails> list2=helpClassifyDetailsService.selectList();
        for(HelpClassifyModel helpClassify:list3){
            List<HelpClassifyDetails> list4=new ArrayList<>();
            for(HelpClassifyDetails helpClassifyDetails:list2){
                if(helpClassify.getId().equals(helpClassifyDetails.getClassifyId())){
                    list4.add(helpClassifyDetails);
                }
            }
            helpClassify.setList(list4);
        }
        return list3;
    }


    @Override
    public HelpClassify selectById(Long id) {
        return helpClassifyRepository.selectById(id);
    }

    @Override
    public List<HelpClassify> selectList(Integer classifyType) {
        return helpClassifyRepository.selectList(classifyType);
    }

    @Override
    public HelpClassify save(HelpClassify helpClassify) {
        return helpClassifyJpaRepository.save(helpClassify);
    }

    @Override
    public void saveAll(List<HelpClassify> list) {
        helpClassifyJpaRepository.saveAll(list);
    }

    @Override
    public HelpClassify update(HelpClassify helpClassify) {
        return helpClassifyJpaRepository.save(helpClassify);
    }

    @Override
    public void deleteById(Long id) {
        helpClassifyJpaRepository.deleteById(id);
    }


    @Override
    @Transactional
    public void deleteByIdList(String ids) {
        String []id=ids.split(",");
        for(String i:id){
            helpClassifyJpaRepository.deleteById(Long.parseLong(i));
        }

    }

}
